class Fabric < ActiveRecord::Base
  belongs_to  :warp_yarn,
              :class_name => "Yarn",
              :foreign_key => "warp_yarn_id"
              
  belongs_to  :weft_yarn, 
              :class_name => "Yarn", 
              :foreign_key => "weft_yarn_id"
              
  belongs_to  :creel_yarn, 
              :class_name => "Yarn", 
              :foreign_key => "creel_yarn_id"
              
  has_many    :rolls
  has_many    :barcodes,  :as => :resource
  
  def after_initialize
    @inventory_state = RollState.find_by_code(4)
  end
  
  def inventory_state
    @inventory_state
  end
  
  def inventory_yards
    self.rolls.sum(:length, :conditions => "roll_state_id = #{@inventory_state.id}")
  end
  
  def inventory_gross_weight
    self.rolls.sum(:gross_weight, :conditions => "roll_state_id = #{@inventory_state.id}")
  end
  
  def inventory_net_weight
    self.rolls.sum(:net_weight, :conditions => "roll_state_id = #{@inventory_state.id}")
  end
  
  def inventory_rolls
    self.rolls.find(:all, :conditions => ["roll_state_id = ?", @inventory_state.id])
  end
    
end
